package com.horizon.manage.site.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.horizon.common.constrants.HorizonResultCodeEnum;
import com.horizon.common.exception.HorizonException;
import com.horizon.common.util.HorizonUtil;
import com.horizon.manage.site.constant.SiteTypeEnum;
import com.horizon.manage.site.converter.SiteEvdConverter;
import com.horizon.manage.site.dto.SiteDTO;
import com.horizon.manage.site.entity.Site;
import com.horizon.manage.site.service.ISiteService;
import com.horizon.manage.site.vo.SiteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.api.ResultCode;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhaoshb
 * @since 2022/12/5 23:51
 */
@RestController
@RequestMapping("/horizon/site")
@AllArgsConstructor
@Api(value = "场地", tags = "场地")
public class SiteController {

    private ISiteService siteService;

    private SiteEvdConverter siteEvdConverter;

    @PostMapping("/create")
    @ApiOperation(value = "创建", notes = "创建")
    public R<Long> create(@RequestBody SiteDTO siteDTO) throws HorizonException {
        Long siteId = siteService.saveSite(siteDTO);
        return R.data(siteId);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public R<Object> delete(@RequestBody Long siteId) throws HorizonException {
        return R.status(siteService.deleteSite(siteId));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
    public R<Object> update(@RequestBody SiteDTO dto) throws HorizonException {
        return R.status(siteService.updateSite(dto));
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "传入cond以及排序条件")
    public R<IPage<SiteVO>> list(@ApiIgnore @RequestParam Map<String, Object> cond, Query query) {
        IPage<Site> pages = siteService.page(Condition.getPage(query), Condition.getQueryWrapper(cond, Site.class));
        return R.data(siteEvdConverter.pageVO(pages));
    }
}
